//
//  HWGameJiamJieCell.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/9/6.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWGameJiamJieCell.h"

@interface  HWGameJiamJieCell()
@property(strong,nonatomic) UILabel *introductionContentLabel;
@property(strong,nonatomic) UIButton *zhanKaiButton;

@end

@implementation HWGameJiamJieCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        [self  setupUI];
        
    }
    
    return  self;
    
}

// 添加控件
- (void)setupUI {
    
    self.introductionContentLabel = [[UILabel alloc]initWithFrame:CGRectMake(18, 0, kScreenW-36, 30)];
    
    NSString *str = @"1111";
    
  //   NSString *str = @"《宠物精灵宝可梦》是一款免费的放置类卡牌游戏，保留了经典的回合制对战策略，主人公立志成为一名神奇宝贝大师，收集强大的精灵打造属于自己的强大队伍！";
    self.introductionContentLabel.text = str;
    self.introductionContentLabel.numberOfLines = 0;
    self.introductionContentLabel.font = [UIFont systemFontOfSize:12];
    self.introductionContentLabel.textColor  = RGB(102, 102, 102);
    
   // self.introductionContentLabel.backgroundColor = [UIColor redColor];
    CGFloat  contentH = [self calculateRowHeight:str fontSize:12];
    DLog(@"这个高度 %f",contentH);
    
    self.introductionContentLabel.frame = CGRectMake(18, 3, kScreenW-36, contentH);

    [self.contentView addSubview: self.introductionContentLabel];
    
    //  42.960938
    
    
    NSString *zhangkai = [HWjianzhiduiObject keylocalization:@"展开全部"];
    
    NSString *shouqiquanbu = [HWjianzhiduiObject keylocalization:@"收起全部"];
    
    self.zhanKaiButton  = [UIButton setButtonWithFrame:CGRectMake((kScreenW-57)/2, contentH+10, 46+4+7, 12) Title:zhangkai Image:@"details_down" Font:12 Color:RGB(34, 141, 255) Target:self Selector:@selector(buttonTap:)];
    [self.zhanKaiButton  setImage:[UIImage imageNamed:@"shouqi_down"] forState:UIControlStateSelected];
    [self.zhanKaiButton setTitle:shouqiquanbu forState:UIControlStateSelected];
    [self.zhanKaiButton  layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:4];
    
    [self.contentView addSubview:self.zhanKaiButton];
    
    
    
    
}


- (void)setDataStr:(NSString *)dataStr {
    
    _dataStr = dataStr;
    
    self.introductionContentLabel.text = dataStr;
    CGFloat  contentH = [self calculateRowHeight:dataStr fontSize:12];
    
    if (self.zhangkan == NO) {
        
        if (contentH > 44) {
            
          //  return  43+12+10+3;
          self.introductionContentLabel.frame = CGRectMake(18, 3, kScreenW-36,  44);
          self.zhanKaiButton.frame = CGRectMake((kScreenW-57)/2, self.introductionContentLabel.bottom+10, 46+4+7, 12);
            self.zhanKaiButton.hidden = NO;
            
            
            
            
        } else {
            
           // return  contentH+12+10+3;
            
            self.introductionContentLabel.frame = CGRectMake(18, 3, kScreenW-36,  contentH);
          //  self.zhanKaiButton.frame = CGRectMake((kScreenW-57)/2, self.introductionContentLabel.bottom+10, 46+4+7, 12);
            self.zhanKaiButton.hidden = YES;
            
            
        }
        
        
    } else {
        
       // self.introductionContentLabel.frame = CGRectMake(18, 3, kScreenW-36,  contentH);
        // return contentH+12+10+3;
        
        self.introductionContentLabel.frame = CGRectMake(18, 3, kScreenW-36,  contentH);
        self.zhanKaiButton.frame = CGRectMake((kScreenW-57)/2, self.introductionContentLabel.bottom+10, 46+4+7, 12);
        self.zhanKaiButton.hidden = NO;
    }
    
    
    
}


- (CGFloat)calculateRowHeight:(NSString *)string fontSize:(NSInteger)fontSize

{
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
    
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(kScreenW-36, MAXFLOAT)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    
    return rect.size.height;
    
}


#pragma mark -----------  点击展开
-(void)buttonTap:(UIButton *)sender {
    
    if (sender.selected) {
        
       sender.selected = NO;
        
         NSDictionary *dict = @{@"rolling":@"0"};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"xianqingtongzhSentimentOnTheSlide" object:nil userInfo:dict];
    
        
    } else {
        
        sender.selected = YES;
        
        
      //  NSString *rolling = [NSString stringWithFormat:@"%ld",leftI];
        
        NSDictionary *dict = @{@"rolling":@"1"};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"xianqingtongzhSentimentOnTheSlide" object:nil userInfo:dict];
        
        
        
        
    }
 
    
    
    
}


@end
